<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/31
 * Time: 9:12
 */

namespace App\Utils;


class Paginater
{


    /**
     * @var int
     */
    public $page = 1;
    /**
     * @var int
     */
    public $page_size = 8;
    /**
     * @var int
     */
    public $total = 0;
    /**
     * @var int
     */
    public $total_page = 0;
    /**
     * @var int
     */
    public $current_size = 0;
    /**
     * @var string
     */
    public $sql;
    /**
     * @var int
     */
    public $time = 0;
    /**
     * @var Array
     */
    public $data = [];

    public function __construct($args = [])
    {

    }

    public function toJson()
    {
        return json_encode($this);
    }

    public function reset()
    {
        $this->setpage(1);
        $this->setPageSize(10);
        $this->setTotal(0);
        $this->setTotalPage(0);
        $this->setData([]);
    }

    public function calculate()
    {
        $this->setTotalPage(ceil($this->getTotal() * 1.0 / $this->getPageSize()));
    }

    public function getSkipSize()
    {
        return ($this->getPage() - 1) * $this->getPageSize();
    }

    /**
     * @return int
     */
    public function getPage()
    {
        return $this->page;
    }

    /**
     * @param int $page
     */
    public function setPage($page)
    {
        $this->page = $page;
    }


    /**
     * @return int
     */
    public function getPageSize()
    {
        if ($this->page_size <= 0) {
            return 10;
        }
        return $this->page_size;
    }

    /**
     * @param int $page_size
     */
    public function setPageSize($page_size)
    {
        $this->page_size = $page_size;
    }

    /**
     * @return int
     */
    public function getTotal()
    {
        return $this->total;
    }

    /**
     * @param int $total
     */
    public function setTotal($total)
    {
        $this->total = $total;
        $this->calculate();
    }

    /**
     * @return int
     */
    public function getTotalPage()
    {
        return $this->total_page;
    }

    /**
     * @param int $total_page
     */
    public function setTotalPage($total_page)
    {
        $this->total_page = $total_page;
    }

    /**
     * @return string
     */
    public function getSql()
    {
        return $this->sql;
    }

    /**
     * @param string $sql
     */
    public function setSql($sql)
    {
        $this->sql = $sql;
    }

    /**
     * @return int
     */
    public function getTime()
    {
        return $this->time;
    }

    /**
     * @param int $time
     */
    public function setTime($time)
    {
        $this->time = $time;
    }

    /**
     * @return Array
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * @param Array $data
     */
    public function setData($data)
    {
        $this->data = $data;
        if (empty($this->data)) {
            $this->current_size = 0;
        } else {
            $this->current_size = count($this->data);
        }
    }

    /**
     * @return int
     */
    public function getCurrentSize()
    {
        return $this->current_size;
    }


    public function __toString()
    {
        return json_encode($this);
    }

}